package com.hx.petsever.controller;

import com.hx.petsever.config.Result;
import com.hx.petsever.entity.Address;
import com.hx.petsever.service.AddressService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @RequestMapping("/insert")
    public Result insert(Address address)
    {
        Result<Object> objectResult = new Result<>();
        HashMap hashMap = addressService.InsertAddress(address.getName(), address.getPhone(), address.getAddress(), address.getUserid(), address.getDefaults());
        if(hashMap.get("state").toString().equals("0"))
        {
            objectResult.fail(hashMap.get("msg").toString());
        }else {
            objectResult.success(hashMap.get("msg").toString());
        }
        return objectResult;
    }

    @RequestMapping("/getAll")
    public Result getAll(@Param("userid") String userid)
    {
        Result<Object> objectResult = new Result<>();
        HashMap<String,Object> map = addressService.getAllbyId(userid);
        if(map.get("state").toString().equals("0"))
            objectResult.fail(map.get("msg").toString());
        else
        {
            objectResult.setData(map.get("msg"));
            objectResult.success();
        }
        return objectResult;
    }

    @RequestMapping("/delete")
    public Result delete(Address address)
    {
        Result<Object> objectResult = new Result<>();
        HashMap hashMap = addressService.DeleteOne(address.getName(), address.getPhone(), address.getAddress(), address.getUserid());
        objectResult.success(hashMap.get("msg").toString());
        return  objectResult;
    }
}
